package consul

import (
	"strings"
)

const (
	rpc CheckType = iota
	http
	https
)

// Client .
type Client interface {
	// Register 注册到服务发现
	Register(Options) (string, error)
	// UnRegister 取消注册
	UnRegister(Options) error
	// Discover 以配置前缀或着色信息等过滤出已经注册的服务
	Discover(config DisconverParams) ([]*ServiceInfo, error)
	// Store 存储key value
	Store(key, value string) error
	// Load 获取key，存在返回key的value和bool值true
	// 否则返回""和bool值false
	Load(key string) (string, bool)
	// Delete 删除key value中的指定key
	Delete(string) error
	// NewLock 新建锁
	NewLock(key string) (Locker, error)
}

// Locker 锁
type Locker interface {
	// Lock 锁，channel可以临时终止
	Lock(<-chan struct{}) error
	// UnLock 释放锁
	UnLock() error
}

// CheckType 健康检查方式 rpc/http/https
type CheckType int

// NewCheckType new health check type
// typ is one of rpc/http/https
// if typ is nil or undefined return rpc
func NewCheckType(typ string) CheckType {
	switch strings.ToLower(typ) {
	case "rpc":
		return CheckType(rpc)
	case "http":
		return CheckType(http)
	case "https":
		return CheckType(https)
	default:
		return CheckType(rpc)
	}
}

// IsRPC return true if type is rpc
// else return false
func (c *CheckType) IsRPC() bool {
	return *c == rpc
}

// IsHTTP return true if type is http
// else return false
func (c *CheckType) IsHTTP() bool {
	return *c == http
}

// IsHTTPS return true if type is https
// else return false
func (c *CheckType) IsHTTPS() bool {
	return *c == https
}

// String return string
func (c *CheckType) String() string {
	switch *c {
	case rpc:
		return "rpc"
	case http:
		return "http"
	case https:
		return "https"
	}

	return "rpc"
}

// Config register config
type Config struct {
	Address string
}

// Options service configuration
type Options struct {
	ServicePort  int
	HealthPort   int
	HealthMethod string
	ServiceName  string
	Type         CheckType
	Tags         []string
	Meta         map[string]string
	Address      string
	Interval     string
	Timeout      string
}

// ServiceInfo service information
type ServiceInfo struct {
	Addr        string
	ServiceID   string
	ServiceName string
	Location    string
	Meta        map[string]string
	Tags        []string
}

type DisconverParams struct {
	Color  string
	Prefix string
	Tag    string
	Suffix bool
}
